package com.hctforgreen.greenservice.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.LearningActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TerminalLearningMaterialsActivity;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.AttrEntity;
import com.hctforgreen.greenservice.ui.adapter.TerminalLearningTypeAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;

/**
 * 终端培训----学习资料下拉弹框
 *
 * @author 390694
 */
public class TerminalLearningTypeListPopupView extends PopupWindow {

    private View mConvertView;
    private Activity mActivity;

    private AttrEntity mCurrentSecondEntity;
    private TerminalLearningMaterialsActivity mSelf;
    private int type;// 0:资料大类别 1:资料小类别

    private String pid;// 只用于获取小类别列表

    /**
     * @param activity
     * @param currentSecondEntity
     * @param self
     * @param type                0:资料大类别 1:资料小类别
     */
    public TerminalLearningTypeListPopupView(Activity activity,
                                             AttrEntity currentSecondEntity, TerminalLearningMaterialsActivity self, int type,
                                             String pid) {
        super(activity);
        mActivity = activity;
        mCurrentSecondEntity = currentSecondEntity;
        mSelf = self;
        this.pid = pid;
        this.type = type;
        initWindow();
    }


    private void initWindow() {
        mConvertView = getLayout();

        this.setContentView(mConvertView);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setWidth(LayoutParams.FILL_PARENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);

        loadDataLst();
    }

    private View getLayout() {
        View layout = mActivity.getLayoutInflater().inflate(
                R.layout.popup_series_list, null);
        return layout;
    }

    private void loadDataLst() {
        final LinearLayout loadLyt = (LinearLayout) mConvertView
                .findViewById(R.id.lyt_default_list_load);
        final LinearLayout reloadLyt = (LinearLayout) mConvertView
                .findViewById(R.id.lyt_default_list_reload);
        final ListView firstLv = (ListView) mConvertView
                .findViewById(R.id.lst_default_list);

        final Button reloadBtn = (Button) mConvertView
                .findViewById(R.id.btn_default_list_reload);
        final Handler handler = new Handler() {
            public void handleMessage(Message msg) {

                HctResult hctResult = new HctResult();
                switch (msg.what) {
                    case HctConstants.STATUS_SUCCESS:

                        loadLyt.setVisibility(View.GONE);
                        reloadLyt.setVisibility(View.GONE);
                        firstLv.setVisibility(View.VISIBLE);

                        hctResult = (HctResult) msg.obj;

                        List<AttrEntity> dataList = (ArrayList<AttrEntity>) hctResult.data;
                        renderDataLst(firstLv, dataList);

                        break;

                    case HctConstants.STATUS_FAILD:
                        loadLyt.setVisibility(View.GONE);
                        reloadLyt.setVisibility(View.VISIBLE);
                        firstLv.setVisibility(View.GONE);
                        reloadBtn.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                loadDataLst();
                            }
                        });
                        break;
                    //20161104 add
                    case HctConstants.STATUS_NO_DATA:
                        loadLyt.setVisibility(View.GONE);
                        reloadBtn.setText(R.string.no_data_tip);
                        reloadLyt.setVisibility(View.VISIBLE);
                        firstLv.setVisibility(View.GONE);

                        break;
                }
            }

        };
        loadLyt.setVisibility(View.VISIBLE);
        reloadLyt.setVisibility(View.GONE);
        new Thread() {
            public void run() {
                Message message = new Message();
                try {
                    HctController controller = new HctController(mActivity);

                    // HctResult result = controller
                    // .getDbAllSeriesWithDbChildSeriesAndBooks(true);
                    HctResult result = null;
                    if (TextUtils.isEmpty(pid)) {// 获取大类别列表
                        result = controller.getTerminalAllStudyType();
                    } else {// 获取小类别列表
                        result = controller.getTerminalChildTypeByTypeId(pid);
                    }
                    if (result.status == HctConstants.STATUS_SUCCESS) {
                        message.what = result.status;
                        message.obj = result;
                    } else {
                        message.what = result.status;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                handler.sendMessage(message);
            }
        }.start();
    }

    private void renderDataLst(ListView firstLv, List<AttrEntity> dataList) {

        new TerminalLearningTypeAdapter(firstLv, mActivity, dataList,
                mCurrentSecondEntity, TerminalLearningTypeListPopupView.this, type);

    }

    public void updateWindow(AttrEntity currentFirstEntity) {
        switch (type) {
            case 0:
                mSelf.initStudyTypeEntity(currentFirstEntity);
                mSelf.initStudyTypeBtn(currentFirstEntity);
                mSelf.clearChildTypeBtn();

                break;
            case 1:
                mSelf.initStudyChildTypeEntity(currentFirstEntity);
                mSelf.initChildTypeBtn(currentFirstEntity);

                break;
            default:
                break;
        }

    }
}
